<script setup lang="ts">
import {ref} from "vue";
import { useRouter,useRoute } from 'vue-router';
import {getEvaluationDetail, submitAssessment} from "@/api/psychological/http";
import {showDialog} from "vant";
import {useResultStore} from "@/stores/evaluationResult";
const  router = useRouter()
const  route = useRoute()
console.log(route.query.name)
let detailList = ref([]);
let total = ref<number>();
let current = ref<number>(1)
getEvaluationDetail(route.query.id).then((res)=>{
    if (res.code === 200){
        detailList.value =res.data.map((item)=>{
            const option = item.option.map((opItem)=>{
                return {...opItem,checked:false}
            })
            return {...item,option}
        })
        total.value = res.data.length
        console.log(detailList.value)
    }
})
const isChecked = (obj)=>{

    detailList.value[current.value-1].option.forEach((item)=>{
        item.checked = false
    })
    obj.checked = true
}
const nextOption = ()=>{
    const flag = detailList.value[current.value-1].option.some((item)=>{
        return  item.checked === true
    })
    if (flag) {
        current.value++
    }else {
        showDialog({ message: '您还没有选择！' });
    }
}
let submitParam = ref({
    studentId:localStorage.getItem('studentId'),
    assessmentId:route.query.id,
    ids:[]
})
const submitOption = ()=>{
    const flag = detailList.value[current.value-1].option.some((item)=>{
        return  item.checked === true
    })
    if (flag) {
        submitParam.value.ids =detailList.value.map((item)=>{
            const checkedOption=  item.option.filter((opItem)=>{
                return opItem.checked === true
            })
            return {
                id:item.id,
                optionId:checkedOption[0].id
            }
        })
        submitAssessment(submitParam.value).then((res)=>{
            if (res.code === 200) {
                router.push({path:'/result',query:{score:res.data.score,analysis:res.data.analysis}})
            }
        })

    }else {
        showDialog({ message: '您还没有选择！' });
    }
}
</script>

<template>
    <div class="evaluation-detail-container">
        <section class="evaluation-detail-top">
            <router-link :to="router.options.history.state.back">
                <van-icon name="arrow-left"/>
                {{route.query.name}}
            </router-link>
        </section>
        <section class="evaluation-detail-body" >
            <div class="progress-box">
                <span>{{ current}}/{{total}}</span>
                <van-progress :percentage="(current/total*100)" stroke-width="16" />
            </div>
            <div class="content-box">
                <p>{{current}}.{{detailList[current-1].title}}</p>
                <ul>
                    <li v-for="item in detailList[current-1].option" :class="item.checked?'active':''" :key="item.id" @click="isChecked(item)">
                        {{item.optname}}
                        <span>{{item.content}}</span>
                    </li>
                </ul>
            </div>

        </section>
        <section class="evaluation-detail-bottom">
            <van-button v-if="current!== 1" type="primary" @click="current--">上一题</van-button>
            <van-button v-if="current!== total" type="primary" @click="nextOption">下一题</van-button>
            <van-button v-if="current === total" type="primary" @click="submitOption">提交</van-button>
        </section>
    </div>
</template>

<style scoped lang="less">
a {
    color:#333333;
}
.evaluation-detail-container {
    background-color: #F8F8F8;
    min-height: 100vh;
    .evaluation-detail-top {
        width: 100%;
        height: 10vw;
        padding: 10px;
        box-sizing: border-box;
    }
    .evaluation-detail-body {
        height:86vh;
        .progress-box {
            padding: 10px 20px;
            display: flex;
            margin: auto;
            align-items: center;
            width: 95%;
            border-radius: 5px;
            height: 10vw;
            background-color: white;
            span {
                margin-right: 15px;
            }
        }
        .content-box {
            padding: 10px 20px;
            margin:10px auto 0;
            border-radius: 5px;
            width: 95%;
            background-color: white;
            li {
                margin-bottom: 10px;
                width: 100%;
                min-height: 10vw;
                padding: 15px;
                border: 1px solid #DDDDDD;
                border-radius: 5px;
                span {
                    margin-left: 10px;
                }
            }
        }
    }
    .evaluation-detail-bottom {
        width: 100%;
        height: 10vh;
        bottom: 30px;
        text-align: center;
        button {
            margin: 5px 10px;
        }
    }
}
/deep/ .evaluation-detail-bottom .van-button {
    width: 150px;
}
/deep/ .van-progress__pivot {
    display: none;
}
/deep/ .van-progress {
    background-color: #CCCCCC;
    width: 100%;
    border-radius: 0;
}
/deep/ .van-progress__portion {
    background-color: #0078D7;
}
.active {
    color: #1590FB;
}
</style>